import sqlite3 from 'sqlite3';
import { open } from 'sqlite';
import path from 'node:path';

let dbPromise = null;

export async function getDbConnection() {
  if (!dbPromise) {
    const filename = process.env.DB_FILE || path.resolve(
      process.cwd(),
      './db/knowledge_app.sqlite'
    );

    dbPromise = open({
      filename,
      driver: sqlite3.Database,
    });
  }

  return dbPromise;
}
